--每日个人答题
--#include "data\functions\Activity\DailyActivity\SceneAnswer.lua"   once         		--每日场景答题
--#include "data\functions\Activity\DailyActivity\PanelAnswer.lua"   once         		--每日多人答题
--#include "data\config\activityconfig\DailyActivity\PersonAnswerConfig.lua" once

--[[
面板答题正在进行中
]]
function InPersonAnswerDoing( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	if dVar.personQuestionIdx then
		return true
	end
	return false
end


--[[
个人答题开始
]]
function PersonAnswerStart( sysarg )
	if  not Actor.checkActorLevel( sysarg, PersonAnswerConfig.enterLevelLimit[2], PersonAnswerConfig.enterLevelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016, ttFlyTip)
		return
	end

	if InSceneAnswerDoing( ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer003, ttFlyTip)
		return
	end

	if InPanelAnswerDoing( ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer004, ttFlyTip)
		return
	end

	if InPersonAnswerDoing( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer005, ttFlyTip)
		return
	end

	--答题状态
	if Actor.hasState(sysarg,esStateAnswer) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer005, ttFlyTip)
		return
	end

	--副本中不能答题
	local fubenId = Actor.getFubenId( sysarg )
	if fubenId > 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer007, ttFlyTip)
		return
	end

	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_PersonAnswer, 0 ) then
		Actor.sendTipmsg( sysarg, Lang.ScriptTips.COM0021, ttFlyTip)
		return false
	end

	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_PersonAnswer, 0, 1 )		--参与次数+1

	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personQuestionIdx = 0			--答题准备

	if not DrawPersonAnswerQuestionFromBank( sysarg, PersonAnswerConfig.answerNum ) then
		print("DrawPersonAnswerQuestion error !")
		Actor.removeState(sysarg, esStateAnswer) 		--离开答题状态
		return
	end

	Actor.addState(sysarg, esStateAnswer) 			--进入答题状态

	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerSendQuestion")
	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerCheck")
	Actor.regScriptTimer(sysarg, 0, PersonAnswerConfig.beginTime*1000, 0, 1, "LoopPersonAnswerSendQuestion")

	Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer001, ttFlyTip)

	local boardStr = Lang.ScriptTips.PersonAnswer006				--注意：字符串中要带有一个 %d
	OpenBoardPanel( sysarg, enBoardPanelStyle_CountDown1, boardStr, PersonAnswerConfig.beginTime )

	Actor.triggerAchieveEvent(sysarg, 44, 14, 1)				--触发个人答题活动成就
	Actor.triggerQuestEvent(sysarg, 45, 9, 1) 					--任务事件-参加个人答题1次
end

--[[
面板答题结束
flag:1-答题结束，2-答题终止
]]
function PersonAnswerStop(  sysarg, flag )
	Actor.removeState(sysarg, esStateAnswer) 		--离开答题状态

	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personQuestionIdx  = nil
	dVar.myAnswer 			= nil
	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerSendQuestion")
	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerCheck")
	
	if flag == 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer002, ttChatWindow+ttFlyTip)
	else
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer008, ttChatWindow+ttFlyTip)
	end
end

--[[
玩家终止答题
]]
function TerminatePersonAnswer( sysarg )
	--print("TerminatePersonAnswer")
	PersonAnswerStop( sysarg, 2 )							--答题终止
	Actor.closeDialogs( sysarg, enLinkAnswerWnd ) 			--关闭答题面板
end

--[[
从题库中抽取题目
]]
function DrawPersonAnswerQuestionFromBank( sysarg, answerNum )
	--print("DrawPersonAnswerQuestionFromBank answerNum="..answerNum)
	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personAnswersIdx = {}
	local personAnswersIdx = CommonAnswerDrawAnswersIdxInBank( PersonAnswerConfig.answerBankId, answerNum )
	for i, answerIdx in ipairs( personAnswersIdx or {}) do
		dVar.personAnswersIdx[i] = answerIdx
		--print("DrawPersonAnswerQuestionFromBank idx="..i..", answerIdx="..answerIdx)
	end

	for i=1, answerNum do
		if not dVar.personAnswersIdx[i] then
			return false
		end
	end
	return true
end

--[[
场景答题发送题目
]]
function LoopPersonAnswerSendQuestion( sysarg )
	--print("LoopPersonAnswerSendQuestion...")
	if not Actor.hasState(sysarg,esStateAnswer) then
		return
	end

	local newQestionIdx = GetPersonAnswerCurQuestionIdx( sysarg ) + 1 			--下一题开始
	SetPersonAnswerCurQuestionIdx( sysarg, newQestionIdx )
	--print("LoopPersonAnswerSendQuestion newQestionIdx="..newQestionIdx)

	local question = GetPersonAnswerCurQuestion( sysarg )
	if not question then
		print("LoopPersonAnswerSendQuestion, no question")
		PersonAnswerStop( sysarg, 1 )			--答题完毕
		return
	end

	local activityId 	= PERSON_ANSWER_ACTIVITY
	local answerArea 	= nil
	local lastTime 		= PersonAnswerConfig.answerCd
	local idx,score 	= GetItemIdxRandNoWeight( question.score ) 		--答题积分
	if score <= 0 then
		score = SceneAnswerConfig.answerAward.rightAward.addPoint
	end
	SetPersonAnswerCurQuestionScore( sysarg, score )

	local closePanel = true				--关闭倒计时面板
	SendCommonAnswerQuestion( sysarg, activityId, newQestionIdx, lastTime, question, score, closePanel )		--下发题目
	SendCommonRankSelfData( sysarg, enCommonRankType_AnswerToday )			--下发本人名次

	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerSendQuestion")
	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerCheck")
	Actor.regScriptTimer(sysarg, 0, PersonAnswerConfig.answerCd*1000, 0, 1, "LoopPersonAnswerCheck")
end

--[[
答题完毕，到发送下一题题目的时间
]]
function LoopPersonAnswerCheck( sysarg )
	if not Actor.hasState(sysarg,esStateAnswer) then
		return
	end

	local questionIdx = GetPersonAnswerCurQuestionIdx( sysarg )
	if questionIdx <= 0 then
		print("LoopPersonAnswerCheck, person answert over")
		return
	end
	--print("LoopPersonAnswerCheck, questionIdx="..questionIdx)
	CheckPersonAnswer( sysarg, questionIdx )			--检查答案

	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerSendQuestion")
	Actor.unregScriptCallback(sysarg, "LoopPersonAnswerCheck")
	Actor.regScriptTimer(sysarg, 0, PersonAnswerConfig.waitCd*1000, 0, 1, "LoopPersonAnswerSendQuestion")
end

function GetPersonAnswerCurQuestionIdx( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	return dVar.personQuestionIdx or 0
end

function SetPersonAnswerCurQuestionIdx( sysarg, newQuestionIdx )
	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personQuestionIdx = newQuestionIdx
end

function GetPersonAnswerCurQuestionScore( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	return dVar.personQuestionScore or 0
end


function SetPersonAnswerCurQuestionScore( sysarg, newScore )
	local dVar = Actor.getDyanmicVar(sysarg)
	dVar.personQuestionScore = newScore
end

--[[
获取当前题目
]]
function GetPersonAnswerCurQuestion( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	local questionIdx = GetPersonAnswerCurQuestionIdx( sysarg )
	local idx = dVar.personAnswersIdx[questionIdx]
	if not idx then
		return nil
	end
	return  GetCommonAnswerQuestionByIdx( PersonAnswerConfig.answerBankId, idx )
end


--[[
判断玩家是否答题正确
questionIdx：第N轮答题
]]
function CheckPersonAnswer( sysarg, questionIdx )
	local question = GetPersonAnswerCurQuestion(sysarg)
	if not question then
		print("CheckPersonAnswer, no question")
		return
	end

	CheckPersonAnswerActorAnswer( sysarg, PERSON_ANSERT_ACTIVITY, questionIdx, question ) 		--检查答案
end

function DoPersonAnswerActorAnswer( sysarg, roundIdx, opType, param )
	if not Actor.hasState(sysarg,esStateAnswer) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.AnswerActivity003, ttFlyTip)
		return
	end

	if opType == ANSWER_OPTYPE_CHOOSE_ANSWER then 			--答题
		local answerIdx = param
		SetPersonAnswerActorAnswer( sysarg, roundIdx, answerIdx )
	elseif opType == ANSWER_OPTYPE_ONEKEY_HIT then			--一键正确
		PersonAnswerOneKeyHit( sysarg, roundIdx )
	elseif opType == ANSWER_OPTYPE_TERMINATE then
		TerminatePersonAnswer( sysarg )
	end
end

--[[
设置玩家选择的答案
myQuestionIdx:第N轮答题
myAnswerIdx ：所选答案
]]
function SetPersonAnswerActorAnswer( sysarg, myQuestionIdx, myAnswerIdx )
	--print("SetPersonAnswerActorAnswer, myQuestionIdx="..myQuestionIdx..", myAnswerIdx="..myAnswerIdx)
	local questionIdx = GetPersonAnswerCurQuestionIdx( sysarg )
	if myQuestionIdx ~= questionIdx then 		--不是本轮题目
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.AnswerActivity001, ttFlyTip)
		return
	end
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.myAnswer then
		dVar.myAnswer = {}
	end

	local hasQuestionIdx = dVar.myAnswer[1] or 0 		--当前轮数是否已经答了
	if hasQuestionIdx == questionIdx then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer009, ttFlyTip)
		return
	end
	
	dVar.myAnswer[1] = myQuestionIdx
	dVar.myAnswer[2] = myAnswerIdx
	SendCommonAnswerOpReply( sysarg, PERSON_ANSWER_ACTIVITY, myQuestionIdx, ANSWER_OPTYPE_CHOOSE_ANSWER, myAnswerIdx )

	LoopPersonAnswerCheck( sysarg )					--立刻检查答案
end

--[[
个人答题，一键正确
myQuestionIdx
]]
function PersonAnswerOneKeyHit( sysarg, myQuestionIdx )
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.myAnswer then
		dVar.myAnswer = {}
	end

	local questionIdx = GetPersonAnswerCurQuestionIdx( sysarg )
	--print("PersonAnswerOneKeyHit, questionIdx="..questionIdx..", myQuestionIdx="..myQuestionIdx)
	local question = GetPersonAnswerCurQuestion( sysarg )
	if not question then
		return
	end

	if myQuestionIdx ~= questionIdx then 		--不是本轮
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.AnswerActivity001, ttFlyTip)
		return
	end

	local hasQuestionIdx = dVar.myAnswer[1] or 0 		--当前轮数是否已经答了
	if hasQuestionIdx == questionIdx then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.PersonAnswer009, ttFlyTip)
		return
	end

	if not PersonAnswerConfig.oneKeyFee then
		return
	end

	if not CheckConsumeCond( sysarg, {PersonAnswerConfig.oneKeyFee} ) then
		return 
	end

	if not DoConsumeCond( sysarg, {PersonAnswerConfig.oneKeyFee}, 1, GameLog.clAnswerOneKeyFee, "one key"  ) then
		return 
	end

	local rightAnswerIdx = GetCommonAnswerRightAnswerIdx( question )	--正确答案
	SetPersonAnswerActorAnswer( sysarg, questionIdx, rightAnswerIdx )
	SendCommonAnswerOpReply( sysarg, PERSON_ANSWER_ACTIVITY, questionIdx, ANSWER_OPTYPE_ONEKEY_HIT, rightAnswerIdx )

	--print("PersonAnswerOneKeyHit, rightAnswerIdx="..rightAnswerIdx)
end

--[[
获取玩家选择的答案
]]
function GetPersonAnswerActorAnswer( sysarg )
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.myAnswer then
		dVar.myAnswer = {}
	end
	local myQuestionIdx = dVar.myAnswer[1] or 0
	local myAnswerIdx 	= dVar.myAnswer[2] or 0
	--print("GetPersonAnswerActorAnswer, myQuestionIdx="..myQuestionIdx..", myAnswerIdx="..myAnswerIdx)
	return myQuestionIdx, myAnswerIdx
end

function CheckPersonAnswerActorAnswer( sysarg, activityId, questionIdx, question )
	local myQuestionIdx, myAnswerIdx = GetPersonAnswerActorAnswer( sysarg )					--玩家答案
	local rightAnswerIdx = GetCommonAnswerRightAnswerIdx( question )						--正确答案
	--print("CheckPersonAnswerActorAnswer, myQuestionIdx="..myQuestionIdx..", myAnswerIdx="..myAnswerIdx..", questionIdx="..questionIdx..
	--	", rightAnswerIdx="..rightAnswerIdx)

	local addScore = 0
	if myQuestionIdx == questionIdx then			--本轮题目
		if rightAnswerIdx > 0 and myAnswerIdx == rightAnswerIdx then
			addScore  = GetPersonAnswerCurQuestionScore( sysarg )		--PersonAnswerConfig.answerAward.rightAward.addPoint
			UpdateAnswerRankPoint( sysarg, enCommonRankType_AnswerToday, addScore )		--加积分
		end
		SendCommonAnswerCheckResult( sysarg, activityId, questionIdx, myAnswerIdx, rightAnswerIdx, addScore )
	else			--非本路题目，按没有回答出来
		SendCommonAnswerCheckResult( sysarg, activityId, questionIdx, 0, rightAnswerIdx, addScore )
	end
end
